package com.margo.rabbit.topic;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConfirmListener;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.ReturnListener;
import com.rabbitmq.client.AMQP.BasicProperties;

public class MargoRabbitTopicProducer {
	
	public static void main(String[] args) throws IOException, TimeoutException {
		for(int i=0;i<5;i++) {
			topicProducer();	
		}
		//topicProducerRoutingKey();
	}
	
	/**
	 * topic模式
	 * @throws IOException
	 * @throws TimeoutException
	 */
	public static void topicProducer() throws IOException, TimeoutException {
		
		String exchange="margo-topic";
		String queue="margo-topic-queue";
		String routingKey="margo-1";
		ConnectionFactory connectionFactory=new ConnectionFactory();
		connectionFactory.setHost("127.0.0.1");
		Connection connection=connectionFactory.newConnection();
		Channel channel=connection.createChannel();
		channel.exchangeDeclare(exchange, BuiltinExchangeType.TOPIC);
		channel.queueDeclare(queue, false, false, false, null);
		channel.queueBind(queue, exchange, routingKey);
		
		channel.confirmSelect();
		
		channel.addConfirmListener(new ConfirmListener() {
			@Override
			public void handleNack(long deliveryTag, boolean multiple) throws IOException {
				System.out.println("失败");
			}
			@Override
			public void handleAck(long deliveryTag, boolean multiple) throws IOException {
				System.out.println("成功");
			}
		});
		channel.addReturnListener(new ReturnListener() {
			
			@Override
			public void handleReturn(int arg0, String arg1, String arg2, String arg3, BasicProperties arg4, byte[] arg5)
					throws IOException {
				System.out.println("return");
			}
		});
		channel.basicPublish(exchange,routingKey, null, "margo".getBytes());
	}
	
	
	/**
	 * topic模式
	 * @throws IOException
	 * @throws TimeoutException
	 */
	public static void topicProducerRoutingKey() throws IOException, TimeoutException {
		
		String exchange="margo-topic";
		String queue="margo-topic-queue";
		String routingKey="margo-topic";
		ConnectionFactory connectionFactory=new ConnectionFactory();
		connectionFactory.setHost("127.0.0.1");
		Connection connection=connectionFactory.newConnection();
		Channel channel=connection.createChannel();
		channel.exchangeDeclare(exchange, BuiltinExchangeType.TOPIC);
		channel.queueDeclare(queue, false, false, false, null);
		channel.queueBind(queue, exchange, routingKey);
		
		channel.confirmSelect();
		
		channel.addConfirmListener(new ConfirmListener() {
			@Override
			public void handleNack(long deliveryTag, boolean multiple) throws IOException {
				System.out.println("失败");
			}
			@Override
			public void handleAck(long deliveryTag, boolean multiple) throws IOException {
				System.out.println("成功");
			}
		});
		channel.addReturnListener(new ReturnListener() {
			
			@Override
			public void handleReturn(int arg0, String arg1, String arg2, String arg3, BasicProperties arg4, byte[] arg5)
					throws IOException {
				System.out.println("return");
			}
		});
		channel.basicPublish(exchange, routingKey, null, "margo".getBytes());
	}
}
